package org.poptest.bootstrap;

import org.poptest.model.Context;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Alan Roche
 *         Date: 01/02/11
 *         Time: 23:10
 */
public class VerifiableBootstrapper implements Bootstrapper {
    private ParallelMode parallelMode;

    public VerifiableBootstrapper() {
    }

    public void setParallelMode(ParallelMode parallelMode) {
        this.parallelMode = parallelMode;
    }

    public static void clear() {
        countsMap.clear();
    }

    public static class Counts {
        public int beforeTestConfigRuns = 0;
        public int beforeTestMethodRuns = 0;
        public int beforeClassRuns = 0;

        public void clear() {
            beforeClassRuns = 0;
            beforeTestConfigRuns = 0;
            beforeClassRuns = 0;
        }
    }

    private static Map<Class<?>, Counts> countsMap = Collections.synchronizedMap(new HashMap<Class<?>, Counts>());


    public static synchronized Counts counts(Class<?> testClass) {
        if (!countsMap.containsKey(testClass)) {
            countsMap.put(testClass, new Counts());
        }
        return countsMap.get(testClass);
    }

    public synchronized  void beforeTestConfiguration(Context context) {
        // For parallel methods, don't run before test config methods.
        if (parallelMode != ParallelMode.METHODS) {
            counts(context.getTestClass()).beforeTestConfigRuns++;
        }
    }

    public synchronized  void beforeTestMethod(Context context) {
        counts(context.getTestClass()).beforeTestMethodRuns++;
    }

    public synchronized  void beforeClass(Context context) {
        counts(context.getTestClass()).beforeClassRuns++;
    }
}
